from aiogram import F
from aiogram_dialog import Dialog, Window
from aiogram_dialog.widgets.input import TextInput
from aiogram_dialog.widgets.kbd import Back, Cancel, ScrollingGroup, Select
from aiogram_dialog.widgets.text import Format

from poker_bot.core.models.enums.currency import Currency
from poker_bot.tg_bot.dialogs.extras import copy_start_data_to_ctx
from poker_bot.tg_bot.dialogs.getters.payments import (
    payment_method_getter,
    payment_methods_getter,
)
from poker_bot.tg_bot.dialogs.reusable_handlers import set_payment_method_id
from poker_bot.tg_bot.dialogs.widgets import LocaleText
from poker_bot.tg_bot.states import CreateWithdrawSG

from .handlers import create_withdraw, set_payment_amount

select_payment_method_window = Window(
    LocaleText("select-payment-method-msg"),
    ScrollingGroup(
        Select(
            Format("{item.currency}"),
            "payment_method_sel",
            lambda method: method.id,
            "payment_methods",
            on_click=set_payment_method_id,
        ),
        width=2,
        height=3,
        id="useritemssel",
        hide_on_single_page=True,
    ),
    Cancel(LocaleText("back-btn")),
    state=CreateWithdrawSG.select_payment_method,
    getter=payment_methods_getter,
)

select_amount_window = Window(
    LocaleText("enter-withdraw-amount", currency_symbol="{currency_symbol}"),
    TextInput("payment_amount", str, on_success=set_payment_amount),
    Back(LocaleText("back-btn")),
    state=CreateWithdrawSG.select_amount,
    getter=payment_method_getter,
)

enter_payment_address_window = Window(
    LocaleText("enter-credit-cart-msg", when=F["currency"] != Currency.usdt),
    LocaleText("enter-btc-address-msg", when=F["currency"] == Currency.usdt),
    TextInput("payment_amount", str, on_success=create_withdraw),
    Back(LocaleText("back-btn")),
    state=CreateWithdrawSG.enter_payment_address,
    getter=payment_method_getter,
)

create_withdraw_dialog = Dialog(
    select_payment_method_window,
    select_amount_window,
    enter_payment_address_window,
    on_start=copy_start_data_to_ctx,
)
